In [ ]:
a

In [ ]:
myfile = open('myfile.txt', 'w')
for i in range(10):
    myfile.write('./textures/wall_%03i.tga\n' % (i+1))

dosomething()
for i in range(10):
    myfile.write('./textures/wall_01_%03i.tga\n' % (i+1))
myfile.close()

In [ ]:
myfile.closed

In [ ]:
myfile.close()

In [ ]:
with open('myfile.txt', 'w') as myfile:
    for i in range(10):
        myfile.write('./textures/wall_%03i.tga\n' % (i+1))
    
    dosomething()
    for i in range(10):
        myfile.write('./textures/wall_01_%03i.tga\n' % (i+1))

In [ ]:
myfile.closed
try: pass except: pass

In [ ]:
myfile = open('myfile.txt', 'w')
try:
    for i in range(10):
        myfile.write('./textures/wall_%03i.tga\n' % (i+1))
    
    dosomething()
    for i in range(10):
        myfile.write('./textures/wall_01_%03i.tga\n' % (i+1))
except NameError:
    myfile.close()

In [ ]:
myfile = open('myfile.txt', 'w')
try:
    for i in range(10):
        myfile.write('./textures/wall_%03i.tga\n' % (i+1))
    
    dosomething()
    for i in range(10):
        myfile.write('./textures/wall_01_%03i.tga\n' % (i+1))
except NameError, e:
    print(e)
    myfile.close()

In [ ]:
try:
    myfile = open('myfile.txt', 'r+')
    for i in range(10):
        myfile.write('./textures/wall_%03i.tga\n' % (i+1))
    
    dosomething()
    for i in range(10):
        myfile.write('./textures/wall_01_%03i.tga\n' % (i+1))
except NameError, e:
    print(e)
    myfile.close()
except IOError, e:
    print(e)
    print('file not exist')
except EOFError, e:
    print(e)
    print('eof error')

In [ ]:
try:
    myfile = open('myfile.txt', 'w')
    for i in range(10):
        myfile.write('./textures/wall_%03i.tga\n' % (i+1))
    
    dosomething()
    for i in range(10):
        myfile.write('./textures/wall_01_%03i.tga\n' % (i+1))
except:
    myfile.close()

In [ ]:
raise Exception('this is an error')

In [ ]:
class openFialed(Exception):
    pass

In [ ]:
raise openFialed("file can't open")

In [ ]:
try:
    myfile = open('myfile.txt', 'r+')
    for i in range(10):
        myfile.write('./textures/wall_%03i.tga\n' % (i+1))
    
    #dosomething()
    for i in range(10):
        myfile.write('./textures/wall_01_%03i.tga\n' % (i+1))
except NameError, e:
    print(e)
    myfile.close()
except IOError, e:
    print(e)
    print('file not exist')
except EOFError, e:
    print(e)
    print('eof error')
else:
    print('in else prosse')

In [ ]:
try:
    myfile = open('myfile.txt', 'r+')
    for i in range(10):
        myfile.write('./textures/wall_%03i.tga\n' % (i+1))
    
    #dosomething()
    for i in range(10):
        myfile.write('./textures/wall_01_%03i.tga\n' % (i+1))
except NameError, e:
    print(e)
except IOError, e:
    print(e)
    print('file not exist')
except EOFError, e:
    print(e)
    print('eof error')
else:
    print('in else prosse')
finally:
    print('in finally prosse')
    myfile.close()

In [ ]:
# --------------------------------------------------------------------------------  
# Copyright (c) 2013 - 2014 Mack Stone. All rights reserved.  
#   
# Permission is hereby granted, free of charge, to any person obtaining a copy  
# of this software and associated documentation files (the "Software"), to deal  
# in the Software without restriction, including without limitation the rights  
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell  
# copies of the Software, and to permit persons to whom the Software is  
# furnished to do so, subject to the following conditions:  
#   
# The above copyright notice and this permission notice shall be included in  
# all copies or substantial portions of the Software.  
#   
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR  
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE  
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER  
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,  
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN  
# THE SOFTWARE.  
# --------------------------------------------------------------------------------
视频下载地址 http://schi.iteye.com/blog/1938511 Notebook下载地址 https://github.com/mackst/myPyTutorialNotebook